/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.fedoraifz.restrequests;

import java.io.IOException;
import java.net.MalformedURLException;

import javax.xml.bind.JAXBException;

import org.apache.log4j.Logger;

import uk.ljmu.fedoraifz.utilities.GeneralUtilities;
import uk.ljmu.fedoraifz.wadl.fedora34.Objects;
import uk.ljmu.fedoraifz.wadl.fedora34.Pid;
import uk.ljmu.fedoraifz.wadl.fedora34.PidDatastreams;

public class FedoraOperationsAPIAImpl implements FedoraOperationsAPIA{
    private static Logger log = Logger.getLogger(FedoraOperationsAPIAImpl.class);
    
    public String findObjects(String terms, String query, Integer maxresults, String sessiontoken, String resultformat) {
	try {
	    Objects objs = new Objects();

	    objs.get(terms, query, maxresults, sessiontoken, resultformat);
	    
	    return GeneralUtilities.convertStreamToString(objs.getRetVal().getInputStream()).trim();

	} catch (JAXBException e) {
	    log.error("Error", e);
	} catch (MalformedURLException e) {
	    log.error("Error", e);
	} catch (IOException e) {
	    log.error("Error", e);
	}
	//Errors
	return null;
    }

    public String listDatastreams(String pid, String format, String asofdatetime) {
	try {
	    if (pid == null || pid.equals(""))
		return null;
	    PidDatastreams pidds = new PidDatastreams(pid);

	    pidds.get(asofdatetime, format);

	    return GeneralUtilities.convertStreamToString(pidds.getRetVal().getInputStream()).trim();
	} catch (JAXBException e) {
	    log.error("Error", e);
	} catch (MalformedURLException e) {
	    log.error("Error", e);
	} catch (IOException e) {
	    log.error("Error", e);
	}


	//Errors
	return null;
    }
    /**
     * 
     * @param pid
     * @param format
     * @param asofdatetime
     * @return
     */
    public String getObjectProfile(String pid, String format, String asofdatetime) {
	try {
	    if (pid == null || pid.equals(""))
		return null;
	    
	    Pid pidObj = new Pid(pid);

	    pidObj.get(asofdatetime, format);
	    String res = GeneralUtilities.convertStreamToString(pidObj.getRetVal().getInputStream()).trim();
	    if (res.equals(pid))
		//Instead of returning 404 not found with errors, the Fedora API returns the requested PID
		//Fix here this error to report an error
		return null;
	    else return res; 
	} catch (JAXBException e) {
	    log.error("Error", e);
	} catch (MalformedURLException e) {
	    log.error("Error", e);
	} catch (IOException e) {
	    log.error("Error", e);
	}

	//Errors
	return null;
    }
    // TODO implement method
    public void closeHttpConnections() {
    }
}
